package java_option.audio_a031.javafx;

import javafx.fxml.FXML;

import java.io.File;

import java_option.audio_a031.audio.AudioPlay;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleGroup;
import javafx.stage.FileChooser;
import javafx.scene.control.RadioButton;

/**
 * 
 * @author java红了 | java_option <br>
 *2020-11-1417:50:04
 */
public class VolumeController {

	@FXML
	private Slider mSlider;

	private File currentAudioFile;

	private AudioPlay audioPlay;

	@FXML
	private RadioButton mType0;
	@FXML
	private RadioButton mType1;
	@FXML
	private RadioButton mType2;
	@FXML
	private RadioButton mType3;

	private String type = null;

	public void initialize() {
		audioPlay = new AudioPlay();

		mSlider.setMin(0);
		mSlider.setMax(100);
		mSlider.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				audioPlay.setVolume(newValue.floatValue());
			}
		});

		ToggleGroup group = new ToggleGroup();
		mType0.setToggleGroup(group);
		mType1.setToggleGroup(group);
		mType2.setToggleGroup(group);
		mType3.setToggleGroup(group);

		group.selectedToggleProperty().addListener(new ChangeListener<Object>() {

			@Override
			public void changed(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
				RadioButton button = (RadioButton) newValue;
				type = button.getText();
				audioPlay.type = type;
			}
		});

//
		mType3.setSelected(true);
		mSlider.setValue(50);
	}

	@FXML
	public void onLoad(ActionEvent event) {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("选择文件");
		String file = "D:\\test";//
		File file1 = new File(file);
		fileChooser.setInitialDirectory(file1);
		fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("只支持wav", "*.wav"));
		this.currentAudioFile = fileChooser.showOpenDialog(null);

		if (this.currentAudioFile != null) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					audioPlay.play(currentAudioFile.getPath(), 30 * 1000 * 1000);
				}
			}).start();
		}
	}

}
